class Solution {
    TreeNode*build(vector<int>& preorder, vector<int>& inorder,int&prev,int inbegin,int inend)
    {
        if(inbegin>inend)
        {
            return nullptr;
        }
        
        TreeNode*root=new TreeNode(preorder[prev]);
        int rooti=inbegin;//标记root的位置
        while(rooti<=inend)
        {
            if(preorder[prev]==inorder[rooti])
            {
                break;
            }
            rooti++;
        }
        prev++;
        root->left=build(preorder,inorder,prev,inbegin,rooti-1);
        root->right=build(preorder,inorder,prev,rooti+1,inend);
        return root;
    }
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        int i=0;//避免产生const，而无法修改
       return build(preorder,inorder,i,0,inorder.size()-1);
    }
};